﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.EmphasizeContinuousMfg.CApECMEmphasizingManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Options;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.EmphasizeContinuousMfg;

internal class CApECMEmphasizingManager
{
  private const TxColorEmphasizePriority emphasizeColorPriority = (TxColorEmphasizePriority) 200;
  private static CApECMEmphasizingManager m_instance;
  private CApECMEmphasizingModel m_model;

  internal static CApECMEmphasizingManager Instance => CApECMEmphasizingManager.m_instance;

  private CApECMEmphasizingManager()
  {
    this.m_model = CApECMEmphasizingModel.Instance;
    if (this.m_model == null)
      throw new Exception(ECMStringTable.MODEL_NOT_INITIALIZED_MSG);
    this.RegisterGeneralEvents();
    foreach (TxSeamMfgFeature key in this.m_model.MfgToItem.Keys)
      this.RegisterMfgEvents(key);
  }

  public static CApECMEmphasizingManager Initialize()
  {
    CApECMEmphasizingManager.m_instance = new CApECMEmphasizingManager();
    return CApECMEmphasizingManager.m_instance;
  }

  public static void DisposeSingleton()
  {
    CApECMEmphasizingManager.m_instance.UnregisterGeneralEvents();
    if (CApECMEmphasizingManager.m_instance.m_model != null)
    {
      foreach (TxSeamMfgFeature key in CApECMEmphasizingManager.m_instance.m_model.MfgToItem.Keys)
        CApECMEmphasizingManager.m_instance.UnregisterMfgEvents(key);
    }
    CApECMEmphasizingManager.m_instance = (CApECMEmphasizingManager) null;
  }

  public void SetWidthToMfgItem(CApECMContinuousMfgItem mfgItem, int width)
  {
    TxSeamMfgFeature mfg = mfgItem.Mfg;
    if (!TxSeamMfgFeature.op_Inequality(mfg, (ITxObject) null))
      return;
    mfg.SetLineWidth(width);
    mfgItem.CurrentWidth = width;
  }

  public void SetWidthToSelectedMfgs()
  {
    foreach (ITxObject selectedContinuousMfg in (Collection<ITxObject>) this.GetSelectedContinuousMfgs())
    {
      TxSeamMfgFeature seamMfg = selectedContinuousMfg as TxSeamMfgFeature;
      if (TxSeamMfgFeature.op_Inequality(seamMfg, (ITxObject) null))
      {
        CApECMContinuousMfgItem mfgItemFromMfg = this.m_model.GetMfgItemFromMfg(seamMfg);
        if (mfgItemFromMfg != null)
          this.SetWidthToMfgItem(mfgItemFromMfg, this.m_model.CurrentWidth);
      }
    }
    TxApplication.RefreshDisplay();
  }

  public void SetWidthToAllMfgs(bool shouldRefreshDisplay)
  {
    foreach (CApECMContinuousMfgItem mfgItem in this.m_model.MfgToItem.Values)
      this.SetWidthToMfgItem(mfgItem, this.m_model.CurrentWidth);
    if (!shouldRefreshDisplay)
      return;
    TxApplication.RefreshDisplay();
  }

  public void SetOriginalWidthToAllMfgs(bool shouldRefreshDisplay)
  {
    foreach (CApECMContinuousMfgItem mfgItem in this.m_model.MfgToItem.Values)
      this.SetWidthToMfgItem(mfgItem, mfgItem.OriginalWidth);
    if (!shouldRefreshDisplay)
      return;
    TxApplication.RefreshDisplay();
  }

  public void EmphasizeMfgColor(
    CApECMContinuousMfgItem mfgItem,
    TxColor color,
    bool shouldRefreshDisplay)
  {
    TxSeamMfgFeature mfg = mfgItem.Mfg;
    if (!TxSeamMfgFeature.op_Inequality(mfg, (ITxObject) null) || !TxColor.op_Inequality(color, (TxColor) null))
      return;
    ITxDisplayableObject displayableObject = (ITxDisplayableObject) mfg;
    this.UnEmphasizeMfgColor(mfgItem);
    TxDisplayableObjectEx.EmphasizeObject(displayableObject, color, (TxColorEmphasizePriority) 200);
    mfgItem.CurrentColor = color;
    if (!shouldRefreshDisplay)
      return;
    TxApplication.RefreshDisplay();
  }

  public void EmphasizeSelectedMfgsColor()
  {
    if (!this.m_model.DoesHaveCurrentColor)
      return;
    foreach (ITxObject selectedContinuousMfg in (Collection<ITxObject>) this.GetSelectedContinuousMfgs())
    {
      TxSeamMfgFeature seamMfg = selectedContinuousMfg as TxSeamMfgFeature;
      if (TxSeamMfgFeature.op_Inequality(seamMfg, (ITxObject) null))
      {
        CApECMContinuousMfgItem mfgItemFromMfg = this.m_model.GetMfgItemFromMfg(seamMfg);
        if (mfgItemFromMfg != null)
          this.EmphasizeMfgColor(mfgItemFromMfg, this.m_model.CurrentColor, false);
      }
    }
    TxApplication.RefreshDisplay();
  }

  public void EmphasizeAllMfgsColor(bool shouldRefreshDisplay)
  {
    if (!this.m_model.DoesHaveCurrentColor)
      return;
    foreach (CApECMContinuousMfgItem mfgItem in this.m_model.MfgToItem.Values)
      this.EmphasizeMfgColor(mfgItem, this.m_model.CurrentColor, false);
    if (!shouldRefreshDisplay)
      return;
    TxApplication.RefreshDisplay();
  }

  public void UnEmphasizeMfgColor(CApECMContinuousMfgItem mfgItem)
  {
    if (!mfgItem.IsEmphasizedColor)
      return;
    TxSeamMfgFeature mfg = mfgItem.Mfg;
    if (!TxSeamMfgFeature.op_Inequality(mfg, (ITxObject) null))
      return;
    TxDisplayableObjectEx.StopEmphasizeObject((ITxDisplayableObject) mfg, mfgItem.CurrentColor, (TxColorEmphasizePriority) 200);
    mfgItem.IsEmphasizedColor = false;
  }

  public void UnEmphasizeSelectedMfgsColor()
  {
    foreach (ITxObject selectedContinuousMfg in (Collection<ITxObject>) this.GetSelectedContinuousMfgs())
    {
      TxSeamMfgFeature seamMfg = selectedContinuousMfg as TxSeamMfgFeature;
      if (TxSeamMfgFeature.op_Inequality(seamMfg, (ITxObject) null))
      {
        CApECMContinuousMfgItem mfgItemFromMfg = this.m_model.GetMfgItemFromMfg(seamMfg);
        if (mfgItemFromMfg != null)
          this.UnEmphasizeMfgColor(mfgItemFromMfg);
      }
    }
    TxApplication.RefreshDisplay();
  }

  public void UnEmphasizeAllMfgsColor(bool shouldRefreshDisplay)
  {
    foreach (CApECMContinuousMfgItem mfgItem in this.m_model.MfgToItem.Values)
      this.UnEmphasizeMfgColor(mfgItem);
    if (!shouldRefreshDisplay)
      return;
    TxApplication.RefreshDisplay();
  }

  public bool DoesAllSelectedMfgsHaveTheSameWidth(out int width)
  {
    bool flag = true;
    TxObjectList selectedContinuousMfgs = this.GetSelectedContinuousMfgs();
    width = -1;
    if (((Collection<ITxObject>) selectedContinuousMfgs).Count > 0)
    {
      foreach (TxSeamMfgFeature seamMfg in (Collection<ITxObject>) selectedContinuousMfgs)
      {
        CApECMContinuousMfgItem mfgItemFromMfg = this.m_model.GetMfgItemFromMfg(seamMfg);
        if (mfgItemFromMfg != null)
        {
          if (width == -1)
            width = mfgItemFromMfg.CurrentWidth;
          if (width != mfgItemFromMfg.CurrentWidth)
          {
            flag = false;
            width = -1;
            break;
          }
        }
      }
    }
    return flag;
  }

  public void UnEmphasizeAllMfgs()
  {
    CApECMEmphasizingManager.m_instance.UnEmphasizeAllMfgsColor(false);
    CApECMEmphasizingManager.m_instance.SetOriginalWidthToAllMfgs(false);
    TxApplication.RefreshDisplay();
  }

  public static TxColor GetColorFromOptions()
  {
    return TxColor.FromBgrColor(TxOptionsEx.ContinuousMfgsEmphasizeColor);
  }

  public static int GetWidthFromOptions() => TxOptionsEx.ContinuousMfgsEmphasizeWidth;

  public static bool GetConsiderColorFromOptions()
  {
    return TxOptionsEx.ConsiderContinuousMfgEmphasizeColor;
  }

  private void RegisterGeneralEvents()
  {
    TxApplication.ActiveDocument.MfgRoot.ItemCreated += new TxObjectRoot_ItemCreatedEventHandler(this.MfgRoot_ItemCreated);
  }

  private void UnregisterGeneralEvents()
  {
    TxApplication.ActiveDocument.MfgRoot.ItemCreated -= new TxObjectRoot_ItemCreatedEventHandler(this.MfgRoot_ItemCreated);
  }

  private void RegisterMfgEvents(TxSeamMfgFeature mfg)
  {
    ((TxMfgFeature) mfg).Deleted += new TxObject_DeletedEventHandler(this.mfg_Deleted);
  }

  private void UnregisterMfgEvents(TxSeamMfgFeature mfg)
  {
    ((TxMfgFeature) mfg).Deleted -= new TxObject_DeletedEventHandler(this.mfg_Deleted);
    TxSeamMfgFeatureEx seamMfgFeatureEx = new TxSeamMfgFeatureEx(mfg);
    if (seamMfgFeatureEx == null)
      return;
    seamMfgFeatureEx.Reloaded -= new TxComponent_ReloadedEventHandler(this.mfgEx_Reloaded);
  }

  private TxObjectList GetSelectedContinuousMfgs()
  {
    TxObjectList selectedContinuousMfgs = new TxObjectList();
    foreach (ITxObject allItem in (Collection<ITxObject>) TxApplication.ActiveSelection.GetAllItems())
    {
      TxSeamMfgFeature txSeamMfgFeature = allItem as TxSeamMfgFeature;
      if (TxSeamMfgFeature.op_Inequality(txSeamMfgFeature, (ITxObject) null))
        ((Collection<ITxObject>) selectedContinuousMfgs).Add((ITxObject) txSeamMfgFeature);
    }
    return selectedContinuousMfgs;
  }

  private void AddedMfgInEmphasizingMode(TxSeamMfgFeature mfg)
  {
    if (!this.m_model.MfgToItem.ContainsKey(mfg))
      this.m_model.AddMfgItem(mfg);
    CApECMContinuousMfgItem mfgItemFromMfg = this.m_model.GetMfgItemFromMfg(mfg);
    if (mfgItemFromMfg == null)
      return;
    this.SetWidthToMfgItem(mfgItemFromMfg, this.m_model.DefaultWidth);
    if (this.m_model.DoesHaveDefaultColor)
      this.EmphasizeMfgColor(mfgItemFromMfg, this.m_model.DefaultColor, false);
    TxApplication.RefreshDisplay();
  }

  private void MfgRoot_ItemCreated(object sender, TxObjectRoot_ItemCreatedEventArgs args)
  {
    TxSeamMfgFeature txSeamMfgFeature = args.Item as TxSeamMfgFeature;
    if (!TxSeamMfgFeature.op_Inequality(txSeamMfgFeature, (ITxObject) null))
      return;
    if (!this.m_model.MfgToItem.ContainsKey(txSeamMfgFeature))
      this.m_model.AddMfgItem(txSeamMfgFeature);
    this.RegisterMfgEvents(txSeamMfgFeature);
    if (this.m_model.DoesHaveDefaultColor)
    {
      CApECMContinuousMfgItem mfgItemFromMfg = this.m_model.GetMfgItemFromMfg(txSeamMfgFeature);
      if (mfgItemFromMfg != null)
        this.EmphasizeMfgColor(mfgItemFromMfg, this.m_model.DefaultColor, true);
    }
    TxSeamMfgFeatureEx seamMfgFeatureEx = new TxSeamMfgFeatureEx(txSeamMfgFeature);
    if (seamMfgFeatureEx == null)
      return;
    seamMfgFeatureEx.Reloaded += new TxComponent_ReloadedEventHandler(this.mfgEx_Reloaded);
  }

  private void mfg_Deleted(object sender, TxObject_DeletedEventArgs args)
  {
    TxSeamMfgFeature txSeamMfgFeature = sender as TxSeamMfgFeature;
    if (!TxSeamMfgFeature.op_Inequality(txSeamMfgFeature, (ITxObject) null))
      return;
    CApECMContinuousMfgItem mfgItemFromMfg = this.m_model.GetMfgItemFromMfg(txSeamMfgFeature);
    if (mfgItemFromMfg != null)
    {
      this.UnEmphasizeMfgColor(mfgItemFromMfg);
      this.m_model.RemoveMfgItem(txSeamMfgFeature);
    }
    this.UnregisterMfgEvents(txSeamMfgFeature);
  }

  private void mfgEx_Reloaded(object sender, TxComponent_ReloadedEventArgs args)
  {
    if (!(sender is TxSeamMfgFeatureEx seamMfgFeatureEx))
      return;
    if (TxSeamMfgFeature.op_Inequality(seamMfgFeatureEx.SeamMfg, (ITxObject) null))
      this.AddedMfgInEmphasizingMode(seamMfgFeatureEx.SeamMfg);
    seamMfgFeatureEx.Reloaded -= new TxComponent_ReloadedEventHandler(this.mfgEx_Reloaded);
  }
}
